<?php

namespace App\Http\Controllers\Home;

use App\Model\Accounts;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use League\Flysystem\Exception;
//结算日期配置
class AccountsController extends CommonController
{
    //

	public function __construct()
	{
		parent::__construct();
		$this->model=new Accounts();
	}

	public function listAccount(Request $request){
		$where['company_id']=$this->company_id;
		try{
			$where['name']=isset($request->name)?$request->name:null;
			$where['start_time']=isset($request->start_time)?$request->start_time:null;
			$where['end_time']=isset($request->end_time)?$request->end_time:null;
			$where['status']=isset($request->status)?$request->status:null;
			$where['status_']=isset($request->status_)?$request->status_:null;
			$data=$this->model->lists($where);
		}catch (Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}

	}
	public function addAccount(Request $request){

		$create_uid=Auth::user()->id;
		$res=$request->input();
//		模拟数据
//			$res=[
//				['name'=>'5号正常结算','type'=>1,'account_day'=>1,'status'=>1],
//				['name'=>'6号正常结算','type'=>2,'account_day'=>10,'status'=>1]
//			];
//		模拟数据结束
		$resArr=[];
		foreach($res as $kk=>$vv){
			$resArr[$kk]['name']=$vv['name'];
			$resArr[$kk]['type']=$vv['type'];
			$resArr[$kk]['account_day']=$vv['account_day'];
			$resArr[$kk]['company_id']=$this->company_id;
			$resArr[$kk]['created_at']=date("Y-m-d H:i:s",time());
			$resArr[$kk]['create_uid']=$create_uid;
			$resArr[$kk]['status']=$vv['status'];
		}
		DB::beginTransaction();
		try{
			$result=DB::table('accounts')->insert($resArr);
			DB::commit();
		}catch (Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->stored($result);
		}else{
			return $this->failed($message);
		}
	}
	public function delAccount(Request $request){
		$type=$this->model->where(['id'=>$request->id])->first()->type;
		try{
			if($request->statu==1){
				$this->model->where(['company_id'=>$this->company_id,'type'=>$type])->update(['statu'=>0]);
			}
			$result=$this->model->where(['id'=>$request->id,'company_id'=>$this->company_id])->update(['statu'=>$request->statu]);
		}catch (Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->updated($result);
		}else{
			return $this->failed($message);
		}
	}
	public function edit($id){
		try{
			$data=$this->model->where(['id'=>$id])->first();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}
	public function editsave_account(Request $request){
		$create_uid=Auth::user()->id;
		try{
			$data=$request->only($this->model->getFillable());
			$data['create_uid']=$create_uid;
			$result=$this->model->where(['id'=>$request->id])->update($data);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->updated($result);
		}else{
			return $this->failed($message);
		}
	}

//	添加结算
	public function add(Request $request){
		$data=$request->input();
		$data['type']=$request->type==1?'正常结算':'奖金结算';
		return $data;
	}
}
